const fs = require("fs")
const artTemplate = require("art-template")
function httpReadFile(path, response) {
    fs.readFile(path, function (error, data) {
        if (error) {
            response.setHeader("content-Type", "text/plain;charset=UTF-8")
            response.write('页面加载错误')
            response.end(error)
        } else {
            response.end(data)
        }
    })
}
function htmlReadWithData(path, response, oldlist) {
    fs.readFile(path, (error, data) => {
        if (error) {
            response.setHeader("content-Type", "text/plain;charset=UTF-8")
            response.write("资源加载错误")
            response.end(error)
        }
        else {
            const html = artTemplate.render(data.toString(), {
                list: oldlist
            })
            response.end(html)
        }
    })
}

function readFile(path) {
    return new Promise((resolve, reject) => {
        fs.readFile(path, function (error, data) {
            if (error) {
                reject(error)
            } else {
                resolve(data)
            }
        })
    })
}

function writeFile(path, content) {
    return new Promise((resolve, reject) => {
        fs.writeFile(path, content, function (error) {
            if (error) {
                reject(error)
            } else {
                resolve("保存成功")
            }
        })
    })
}
module.exports = {
    httpReadFile,
    htmlReadWithData,
    readFile,
    writeFile
}